//照片删除
$(".pic_list").on("click","span.iconfont",function(){
	$(this).parents("li").fadeOut(function(){
		$(this).remove();
	})
})
//静态添加属性
$(".addTypeValueDetail").click(function(){
	var modal = $(this).parents(".modal"),
	 	typeValue = modal.find(".typeValue").val(),
		typeName = modal.find(".typeName").val(),
		span = 		'<span class="btn btn-default">'+
		    			'<span class="typeValueDetail">'+typeValue+'</span>'+
		    		'<span class="typeValueDetailRemove">X</span>';
		if(typeName==""||typeValue==""){
			return false;
		}
		modal.find(".type-value-container").append(span);
		modal.find(".typeValue").val("")
})
$(".typeValue").on("keydown",function(e){
	if(e.keyCode=="13"){
		$(".addTypeValueDetail").click();
	}
})

$(".modal").on("click",".typeValueDetailRemove",function(){
	$(this).parent().remove();
})
//修改属性
$(".value-container").on("click",".btn",function(){
	var id = this.id;
	var type_name = $(this).html();
	var modal = null;
	var _this = $(this);
	if($(this).hasClass("btn-info")){
		modal = $("#correctTypeModal")
		modal.find(".typeName").val(type_name);
		modal.find(".type-value-container").html("");
		modal.attr("data-to",id);
		var vals = JSON.parse(_this.attr("data-val"));
		for(var i = 0; i<vals.length;i++){
			var html ='<span class="btn btn-default">'+
						'<span class="typeValueDetail">'+vals[i]+'</span>'+
						'<span class="typeValueDetailRemove">X</span>'+
					  '</span>'	;
		modal.find(".type-value-container").append(html);
		if($(this).attr("disabled")=="disabled"){
			modal.find(".btn").attr("disabled","disabled");
			modal.find(".type-value-container").find(".typeValueDetailRemove").remove()
		}
		}
	}else{
		modal = $("#addTypeModal");
		modal.find(".typeName").val("");
		modal.find(".typeValue").val("");
		modal.find(".type-value-container").html("");
	}
})
$("#correctTypeModal .confirm-type").click(function(){
	var modal =$(this).parents(".modal"),
		id  = modal.attr("data-to"),
		typeName = modal.find(".typeName").val(),
		vals = [];
		modal.find(".type-value-container .btn-default").each(function(){
			vals.push($(this).find(".typeValueDetail").html());
		})
		if(vals.length==0||typeName==""){
			alert("输入正确信息");
			return false;
		}
		var json = JSON.stringify(vals);
		$("#"+id).attr("data-val",json);
		$("#"+id).html(typeName);
		modal.find(".close").click();
})

//点击保存
$("#addTypeModal .confirm-type").click(function(){
	var modal = $(this).parents(".modal"),
	 	typeValue = modal.find(".typeValue").val(),
		typeName = modal.find(".typeName").val(),
		container = modal.find(".type-value-container"),
		id = getUuid(),
		vals = [];
		modal.find(".type-value-container .btn-default").each(function(){
			vals.push($(this).find(".typeValueDetail").html());
		})
		if(vals.length==0||typeName==""){
			alert("输入正确信息");
			return false;
		}
		var span = $('<span class="btn btn-info" data-toggle="modal" data-target="#correctTypeModal" id="'+id+'">'+typeName+'</span>');
		$("#add_type").before(span);
		var json = JSON.stringify(vals);
		span.attr("data-val",json);
		modal.find(".close").click();
})
//删除属性
$("#correctTypeModal").on("click",".remove-type",function(){
	var id = $("#correctTypeModal").attr("data-to");
	$("#"+id).remove();
	$("#correctTypeModal .close").click();
})
//生成规格
$("#confirmTypeModal .confirm-add-type").click(function(){
//	$(".value-container").find(".btn-info")
	var zz=[];//此处数组保存属性值
	var html='';
	var result=new Array();//属性值组合结果保存到这个数组
	var jsonResult = new Array();//UUID组合结果保存到这个数组
	var yy=[];//此数组保存uuid
	if($(".value-container").find(".btn-info").length==0){
		alert("请先增加属性")
	}else{
		$(".value-container").find(".btn-info").each(function(){
			//获取属性值
			var json = JSON.parse($(this).attr("data-val"));
			var $this = $(this);
			var id = $(this).attr("id");
			var attrJson = [];
			zz.push(json);
			console.log(json.length);
			//按属性值顺序生成UUID
			for(var i =0;i<json.length;i++){
				var obj = {};
				obj[id]=getUuid();
				attrJson.push(obj);
			}
			//把uuid放在按钮中，方便校正
			$this.attr("data-uuid",JSON.stringify(attrJson))
			yy.push(attrJson);
		})
			//递归组合uuid
			toResult(0,yy,jsonResult);
			console.log(jsonResult)
			//递归组合属性值
			toResult(0,zz,result);
			for(var i =0;i<result.length;i++){
				html+="<tr><td>"
				var attrjson2={};
				var typeList = [];
				for(var j = 0;j<result[i].length;j++){
						attrjson2 = $.extend(attrjson2,jsonResult[i][j]);
						typeList.push(result[i][j]);
						html+="<span>"+
								result[i][j]+
							  "</span>"
					}
					var obj =  {
						"typeList":typeList
					}
					var attrjson2String = JSON.stringify(attrjson2);
					html+="<span class='type-uuid' data-type="+JSON.stringify(obj)+">"+attrjson2String+"</span></td>"+
						  "<td>"+
						  	"<input class='form-control'/>元"+
						  "</td>"+
						  "<td>"+
						  	"<input class='form-control'/>元"+
						  "</td>"+
						  "<td>"+
						  	"<input class='form-control'/>"+
						  "</td>"+
						  "<td>"+
						  	"<input class='form-control' type='checkbox'/>"+
						  "</td></tr>"
					
			}
			$(".set-type tbody").html(html);
			//禁止按钮弹框
			$(".value-container").find(".btn").attr("disabled","disabled");
			$(".add_type_confirm").attr("disabled","disabled");
	}
	$("#confirmTypeModal .close").click();
	$("#sidebar-left").height($("#content").height()+50);
})
//递归
function toResult(arrIndex,zz,nArr,aresult)
{
  if(arrIndex>=zz.length) {nArr.push(aresult);return;};
  var aArr=zz[arrIndex];
  if(!aresult) aresult=new Array();
  for(var i=0;i<aArr.length;i++)
  {
    var theResult=aresult.slice(0,aresult.length);
    theResult.push(aArr[i]);
    toResult(arrIndex+1,zz,nArr,theResult);
  }
}
//uuid
function getUuid(){
   var len=32;//32长度
   var radix=16;//16进制
   var chars='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');var uuid=[],i;radix=radix||chars.length;if(len){for(i=0;i<len;i++)uuid[i]=chars[0|Math.random()*radix];}else{var r;uuid[8]=uuid[13]=uuid[18]=uuid[23]='-';uuid[14]='4';for(i=0;i<36;i++){if(!uuid[i]){r=0|Math.random()*16;uuid[i]=chars[(i==19)?(r&0x3)|0x8:r];}}}
   return uuid.join('');
}    